package com.cysyz.mylib.tplgenerator.impl.struts2;

import com.cysyz.mylib.nestedconstructparser.NestedconstructParser;
import com.cysyz.mylib.nestedconstructparser.Tag;
import com.cysyz.mylib.nestedconstructparser.impl.BaseNestedconstructParser;
import com.cysyz.mylib.tplgenerator.ReplaceAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by emall on 2014/8/18.
 */
public class IfReplaceAdapter implements ReplaceAdapter {
    private NestedconstructParser nestedconstructParser = new BaseNestedconstructParser();

    @Override
    public StringBuffer process(StringBuffer stringBuffer) {
        List<Tag> tags = nestedconstructParser.parse(stringBuffer.toString(), "<s:if[\\w\\W]*?>", "</s:if>", "<s:elseif[\\w\\W]*?>", "</s:elseif>", "<s:else[\\w\\W]*?>", "</s:else>");

        Pattern ifStartPattern = Pattern.compile("<s:if\\s+test=\"([\\w\\W]*?)\">");
        Pattern ifEndPattern = Pattern.compile("</s:if>");
        Pattern elseIfStartPattern = Pattern.compile("<s:elseif\\s+test=\"([\\w\\W]*?)\">");
        Pattern elseIfEndPattern = Pattern.compile("</s:elseif>");
        Pattern elseStartPattern = Pattern.compile("<s:else[\\w\\W]*?>");
        Pattern elseEndPattern = Pattern.compile("</s:else>");
        List<IfTag> ifTags = new ArrayList<IfTag>();
        int currentDeep = 0;
        int eleseIfBodyStart = 0;
        int elseBodyStart = 0;
        for (Tag tag :tags) {
            String tagValue = tag.getValue();
            Matcher ifStartMatcher = ifStartPattern.matcher(tagValue);
            Matcher elseIfStartMatcher = elseIfStartPattern.matcher(tagValue);
            if (ifStartMatcher.matches()) {
                IfTag ifTag = new IfTag();
                ifTag.setStartPos(tag.getBeginPos());
                ifTags.add(ifTag);
                ifTag.setIfConds(ifStartMatcher.group(1));
                if (tag.getDeep() - 1 == currentDeep) {
                    ifTag.setParentIfTag(ifTags.get(ifTags.size() - 2));
                    currentDeep++;
                }
            } else if (ifEndPattern.matcher(tagValue).matches() && tag.getDeep() == currentDeep) {
                ifTags.get(ifTags.size() - 1).setEndPos(tag.getEndPos());
                IfTag ifTag = null;
                if (currentDeep > 0 ) {
                    ifTag = ifTags.get(ifTags.size() - 1).getParentIfTag();
                } else {
                    ifTag = ifTags.get(ifTags.size() - 1);
                }

            } else if (elseIfStartMatcher.matches()) {
                IfTag ifTag = new IfTag();
                eleseIfBodyStart = tag.getEndPos();
                ifTag.setIfConds(elseIfStartMatcher.group(1));
                ifTags.get(ifTags.size() - 1).addElesif(ifTag);
            } else if(elseIfEndPattern.matcher(tagValue).matches()) {
                List<IfTag> elesifs = ifTags.get(ifTags.size() - 1).getElesifs();
                elesifs.get(elesifs.size() -1).setBody(stringBuffer.substring(eleseIfBodyStart, tag.getBeginPos()));
                ifTags.get(ifTags.size() - 1).setEndPos(tag.getEndPos());
            } else if (elseStartPattern.matcher(tagValue).matches()) {
                elseBodyStart = tag.getEndPos();
            } else if (elseEndPattern.matcher(tagValue).matches()) {
                ifTags.get(ifTags.size() - 1).setEleseBody(stringBuffer.substring(elseBodyStart, tag.getBeginPos()));
                ifTags.get(ifTags.size() - 1).setEndPos(tag.getEndPos());
            }
        }

        return stringBuffer;
    }
}

class IfTag {
    private int startPos;
    private int endPos;
    private IfTag parentIfTag;
    private String ifConds;
    private String body;
    private List<IfTag> elesifs = new ArrayList<IfTag>();
    private String eleseBody;

    public String generate() {
        return null;
    }

    public String getIfConds() {

        return ifConds;
    }

    public void setIfConds(String ifConds) {
        this.ifConds = ifConds;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public List<IfTag> getElesifs() {
        return elesifs;
    }

    public void setElesifs(List<IfTag> elesifs) {
        this.elesifs = elesifs;
    }

    public void addElesif(IfTag elesif) {
        elesifs.add(elesif);
    }

    public String getEleseBody() {
        return eleseBody;
    }

    public IfTag getParentIfTag() {
        return parentIfTag;
    }

    public void setParentIfTag(IfTag parentIfTag) {
        this.parentIfTag = parentIfTag;
    }

    public void setEleseBody(String eleseBody) {
        this.eleseBody = eleseBody;

    }

    public int getStartPos() {
        return startPos;
    }

    public void setStartPos(int startPos) {
        this.startPos = startPos;
    }

    public int getEndPos() {
        return endPos;
    }

    public void setEndPos(int endPos) {
        this.endPos = endPos;
    }
}
